<?php
load_info_var($cf_controller);	   //加载smarty变量
load_file('goods_class', LIB_PATH);
$main = new Goods($db);
$id = isset($_POST['id']) ? intval($_POST['id']) : intval($_GET['id']);
$self = isset($_POST['self']) ? trim($_POST['self']) : trim($_GET['self']);
$return_page = $cf_pre_page ? $cf_pre_page : $cf_self_page;

require_once(INFO_PATH.'cf_brand.php');
require_once(INFO_PATH.'cf_var_cate.php');
require_once(INFO_PATH.'cf_expressfee.php');
require_once(INFO_PATH.'cf_color.php');
switch ($cf_action) {
    case "index":	
	$smarty->mypage($cf_controller, $cf_action, 0);
	$self = isset($_POST['self']) ? trim($_POST['self']) : urldecode($_GET['self']);	
	$skey = isset($_POST['skey']) ? trim($_POST['skey']) : urldecode($_GET['skey']);
	$cate1 = isset($_POST['cate1']) ? trim($_POST['cate1']) : urldecode($_GET['cate1']);
	$cate2 = isset($_POST['cate2']) ? trim($_POST['cate2']) : urldecode($_GET['cate2']);
	$brand = isset($_POST['brand']) ? trim($_POST['brand']) : urldecode($_GET['brand']);
	$start = isset($_POST['start']) ? trim($_POST['start']) : urldecode($_GET['start']);
	$end = isset($_POST['end']) ? trim($_POST['end']) : urldecode($_GET['end']);	
	$where = '1';
	
	if (is_numeric($self)) {
	    $where .= " and goods_status = '" . $self . "'";
	}
	if ($skey) {
	    $where .= " and goods_name like '%" . $skey . "%'";
	}	
	if ($start && $end) {
            $where .= " AND DATE_FORMAT(goods_create_time,'%Y-%m-%d')  BETWEEN '".$start."' AND '".$end."'";
    }
	
	if ($cate1 && $cate2) {
		$where .= " and goods_cate_pid = '" . $cate1 . "' and goods_cate_id = '".$cate2."'";
	}
	if ($brand) {
		$where .= " and goods_brand_id = '" . $brand . "'";
	}

	load_file('page_class', LIB_PATH);
	$MyPage = new Mypage($db);
	$field = '*';
	$data_ary = $MyPage->from_sql($field, $main->get_table_name(), $where, "goods_is_index DESC,goods_id DESC", intval($_GET['cpage']), "20");
	$page = $MyPage->get_page($data, $cf_self_page . '?dpt=' . $cf_dpt .'&se;f=' . $self. '&skey=' . $skey . '&cate=' . $cate . '&brand=' . $brand . '&start=' . $start.'&end='.$end);
	if($data_ary){
		foreach($data_ary as $k=>$v){			
			$data_ary[$k]['cate_title'] = $cf_var_cate[$v['goods_cate_pid']]['title'].' -> '.$cf_var_cate[$v['goods_cate_pid']]['child'][$v['goods_cate_id']]['title'];
		}
	}
	//dump($data_ary);
	//echo $MyPage->get_sql();
	//dump($data_ary);
	break;
    case "add":
	if ($cf_ispost) {	
		
		$goods_color = implode(",",$_POST['goods_color']);
		$goods_images = implode(",",$_POST['goods_images']);
		$item_title = $_POST['item_title'];
		if($item_title){
			$item_price = $_POST['item_price'];			
			$item_original_price = $_POST['item_original_price'];
			$item_board_price = $_POST['item_board_price'];
			$item_image = $_POST['item_image'];
			$item_ary = array();
			foreach($item_title as $k=>$v){
				$item_ary[$k]['title'] = $v;
				$item_ary[$k]['price'] = $item_price[$k];
				$item_ary[$k]['original_price'] = $item_original_price[$k];
				$item_ary[$k]['board_price'] = $item_board_price[$k];
				$item_ary[$k]['image'] = $item_image[$k];
					
			}			
			$item_ary = serialize($item_ary);
		}
		
		$surcharge_title = $_POST['surcharge_title'];
		if($surcharge_title){
			$surcharge_price = $_POST['surcharge_price'];
			$surcharge_free_number = $_POST['surcharge_free_number'];
			$item_ary = array();
			foreach($surcharge_title as $k=>$v){
				$item_ary[$k]['title'] = $v;
				$item_ary[$k]['price'] = $surcharge_price[$k];
				$item_ary[$k]['free_number'] = $surcharge_free_number[$k];
					
			}			
			$surcharge_ary = serialize($item_ary);
		}
		
		$data = $db->getFormData($main->get_table_name(),$_POST);
		$data['goods_color'] = $goods_color;
		$data['goods_images'] = $goods_images;
		$data['goods_property_item'] = $item_ary;
		$data['goods_surcharge_item'] = $surcharge_ary;
		$data['goods_create_time'] = date("Y-m-d H:i:s");
		$data['goods_creator'] = $cf_admin_info['admin_name'];		
		
		
	    $result = $main->insert_data($data);
		
	    if ($result) {
		$return_page = $_SESSION['pre_url'] ? $_SESSION['pre_url'] : $cf_self_page;
		unset($_SESSION['pre_url']);
		$more = $_SERVER['HTTP_REFERER'] ? '<a onclick="my_close()" href="' . $_SERVER['HTTP_REFERER'] . '">[继续添加]</a>' : '';
		my_jump('添加成功' . $more, '', $return_page, 3);
	    } else {
		my_jump('添加失败');
	    }
	} else {

	    $smarty->mypage($cf_controller, $cf_action, 0);
	    $_SESSION['pre_url'] = $cf_pre_page ? $cf_pre_page : '';			
	}

	break;
    case "edit":
	if ($cf_ispost) {	
			
		$goods_color = implode(",",$_POST['goods_color']);
		$goods_images = implode(",",$_POST['goods_images']);
		
		$item_title = $_POST['item_title'];
		if($item_title){
			$item_price = $_POST['item_price'];
			$item_original_price = $_POST['item_original_price'];
			$item_board_price = $_POST['item_board_price'];
			$item_image = $_POST['item_image'];
			$item_ary = array();
			foreach($item_title as $k=>$v){
				$item_ary[$k]['title'] = $v;
				$item_ary[$k]['price'] = $item_price[$k];
				$item_ary[$k]['original_price'] = $item_original_price[$k];
				$item_ary[$k]['board_price'] = $item_board_price[$k];
				$item_ary[$k]['image'] = $item_image[$k];					
			}			
			$property_item = serialize($item_ary);
		}
		
		$surcharge_title = $_POST['surcharge_title'];
		if($surcharge_title){
			$surcharge_price = $_POST['surcharge_price'];
			$surcharge_free_number = $_POST['surcharge_free_number'];
			$item_ary = array();
			foreach($surcharge_title as $k=>$v){
				$item_ary[$k]['title'] = $v;
				$item_ary[$k]['price'] = $surcharge_price[$k];
				$item_ary[$k]['free_number'] = $surcharge_free_number[$k];
					
			}			
			$surcharge_ary = serialize($item_ary);
		}
		
		$data = $db->getFormData($main->get_table_name(),$_POST);
		$data['goods_color'] = $goods_color;
		$data['goods_images'] = $goods_images;
		$data['goods_property_item'] = $property_item;
		$data['goods_surcharge_item'] = $surcharge_ary;
		$data['goods_edit_time'] = date("Y-m-d H:i:s");
		$data['goods_editor'] = $cf_admin_info['admin_name'];		
	    $result = $main->update_data($data, "goods_id='$id'");

	    if ($result) {
		$return_page = $_SESSION['pre_url'] ? $_SESSION['pre_url'] : $cf_self_page;
		unset($_SESSION['pre_url']);
		    my_success('修改成功', '', $return_page, 3);
	    } else {
		    my_error('修改失败');
	    }
	} else {		 
	    $smarty->mypage($cf_controller, $cf_action, 0);
	    $_SESSION['pre_url'] = $cf_pre_page ? $cf_pre_page : '';
		$row = $main->get_data_row($id);
	    if (!$row)
		my_jump('沒有此信息');
		if($row['goods_color']){
			$temp_ary = explode(',',$row['goods_color']);
			$row['selected_color'] = array();
			foreach($temp_ary as $v){
				$row['selected_color'][$v] = $v;	
			}
		}
		
		if($row['goods_tags']){
			$tags_ary = explode(',',$row['goods_tags']);
			$row['goods_tags'] = '';
			foreach($tags_ary as $v){
				$row['goods_tags'] .= "'".$v."',";				
			}
			trim($row['goods_tags'],',');
			
		}
		if($row['goods_images']){
			$row['goods_images'] = explode(',',$row['goods_images']);
		}
		if($row['goods_property_item']){
			$row['goods_property_item'] = unserialize($row['goods_property_item']);
		}else{
			unset($row['goods_property_item']);
		}
		
		if($row['goods_surcharge_item']){
			$row['goods_surcharge_item'] = unserialize($row['goods_surcharge_item']);
		}else{
			unset($row['goods_surcharge_item']);	
		}		
	   
	}
	break;
    case "del":
	$result = $main->del_data($id);
	if ($result) {
	    my_jump('删除成功', '', $return_page, 1);
	} else {
	    my_jump('删除失败!');
	}

	break;
	case "detail":
	$smarty->mypage($cf_controller, $cf_action, 0);
	$row = $main->get_data_row($id);
	if (!$row)
	my_jump('沒有此信息');
	if($row['goods_photo']){
    $img_ary = explode("|",$row['goods_photo']);
		foreach($img_ary as $v){
			$img_list .= '<a href="'.$v.'" rel="lightbox-group" title="'.strip_tags($text_ary[$k]).'"><img src="'.get_thumb_name($v).'" height="30" /></a>&nbsp;&nbsp;'; 
		}
	}
	$row['img'] = $img_list;
	$row['cate'] = get_cate($row['goods_cate'],$row['goods_sub_cate']);
	if($row['goods_package'])
	$row['package'] = json_decode( stripslashes($row['goods_package']),true);
	if($row['goods_options'])
	$row['options'] = json_decode( stripslashes($row['goods_options']),true);
	$row['goods_content'] = htmlspecialchars_decode($row['goods_content']);
	
	break;  
    case "all":	
		$alltype = $_POST['alltype'];
		$idlist = $_POST['idlist'];
		if($idlist){
			$list = implode(",",$idlist);
			switch($alltype){
				case "allhome"://商品列表中首页显示
					$data = array('goods_is_index'=>'1');
					$result = $main->update_data($data,"goods_id in (".$list.") and goods_is_index='0' ");
				break;
				case "cancelallhome"://取消商品列表中首页显示
					$data = array('goods_is_index'=>'0');
					$result = $main->update_data($data,"goods_id in (".$list.") and goods_is_index='1' ");	
				break;				
				default:
					my_jump('请指定操作方式');
				break;
			}	
			if($result)
				my_jump('操作完成','',$return_page,2);
			else
				my_jump('操作失败');
			
		}else{
			my_jump('输入的参数有误');	
		}
	break;
	case "chooseimage":
		$id = intval($get['id']);
		$images = $_COOKIE['images'];			
		$data_ary = explode(',',$images);	
		$smarty->mypage('chooseimage', $cf_action, 0);
	break;
	case "copy"://复制商品信息
		$row = $main->get_data_row($id);
		unset($row['goods_id']);
		unset($row['goods_index_image']);//不复制图片信息
		unset($row['goods_images']);
		$row['goods_create_time'] = date("Y-m-d H:i:s");
		$row['goods_edit_time'] = '';
		$row['goods_editor'] = '';
		
		$result = $main->insert_data($row);
		
	    if ($result) {
			$return_page = $_SESSION['pre_url'] ? $_SESSION['pre_url'] : $cf_self_page;
			unset($_SESSION['pre_url']);	
			my_jump('复制成功', '', $return_page, 3);
	    } else {
			my_jump('复制失败');
	    }		
	break;
	default:
	my_jump('非法操作!');
	break;
}


$smarty->assign(array(
    'id' => $id,
    'row' => $row,
    'data_ary' => $data_ary,
    'page' => $page,
    'self' => $self,
    'skey' => $skey, 
    'cate1' => $cate1,
	'cate2' => $cate2,
    'brand' => $brand,
	'start' => $start,
	'end' => $end,
	'brand_ary' => add_array_item(array('' => '请选择品牌'), $cf_brand),	
	'expressfee_ary' => add_array_item(array('' => '请选择邮费模板'), $cf_expressfee),
	'color_ary' => add_array_item(array('' => '请选择颜色'), $cf_color),
));

$smarty->mydisplay();
?>